package com.example.a02back.Aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.example.a02back.common.APIResponse;
import com.example.a02back.entity.User;
import com.example.a02back.mapper.UserMapper;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.impl.PublicClaims;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;

/**
 * @ClassName VerifyToken
 * @Description
 * @Author SimpChaos
 * @Date 2022/7/7 9:07
 * @Version 1.0
 **/
@Component
@Aspect
@Order(0)
public class VerifyToken {

    @Resource
    private UserMapper usermapper;

    @Pointcut("execution(public * com.example.a02back.controller.UserController..*(..))")
    public void generalCut() {
    }
    @Pointcut("execution(public * com.example.a02back.controller.QueryController..*(..))")
    public void CutQuery() {
    }
    @Pointcut("execution(public * com.example.a02back.controller.TradeController..*(..))")
    public void CutTrade() {
    }
    @Pointcut("execution(public * com.example.a02back.controller.UpdateController..*(..))")
    public void CutUpdate() {
    }
    @Pointcut("execution(public * com.example.a02back.controller.PublishController..*(..))")
    public void CutPublish() {
    }
    @Pointcut("execution(public * com.example.a02back.controller.TestController..*(..))")
    public void CutVoid(){

    }



    @Around("CutVoid()")
    public Object doAroundOperation(ProceedingJoinPoint proceedingjoinpoint) throws Throwable{
        Object[] args = proceedingjoinpoint.getArgs();
        HttpServletRequest request = (HttpServletRequest) args[0];
        HttpServletResponse response = (HttpServletResponse) args[1];
        String nowtoken = request.getHeader("Token");
        response.setHeader("Access-Control-Expose-Headers","State,Token");
        if(nowtoken == null) {
            response.setHeader("State","UNAUTHORIZEDACCESS");
            return APIResponse.fail("Invalid Token");//查不到token说明还没登录
        }
        Date date=JWT.decode(nowtoken).getClaim(PublicClaims.EXPIRES_AT).asDate();
        Date nowdate=new Date();
        if(date.before(nowdate))	return APIResponse.fail("Invalid Token");//token过期
        String openid = JWT.decode(nowtoken).getClaim("openid").asString();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getOid,openid);
        User user1 = usermapper.selectOne(wrapper);
        if(user1 == null) {
            response.setHeader("State","INVALIDTOKEN");
            return APIResponse.fail("Invalid Token");
        }
        response.setHeader("State","ALLOWED");
       // response.setHeader("Token",tokenService.getToken(username, spassword));
        return proceedingjoinpoint.proceed(args);
    }


}
